package com.aplink.generic;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aplink.generic.bitmap.ImageFetcher;
import com.aplink.generic.pojo.controlspec.GallerySpec;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.generic.widgets.hacky.RecyclingImageView;
import com.aplink.sg.R;

public class PhotoFragment extends Fragment {
	private static final String DEFAULT_IMAGE = "DEFAULT_IMAGE";
	private static final String IMAGE_URL = "IMAGE_URL";
	private static final String NO_PHOTO = "NO_PHOTO";
	private static final String POSITION = "PHOTO_POSITION";

	public static PhotoFragment newInstance(final String imageURL,
			final GallerySpec gallerySpec, String position) {
		final PhotoFragment fragment = new PhotoFragment();
		final Bundle bundle = new Bundle();
		bundle.putString(PhotoFragment.IMAGE_URL, imageURL);
		bundle.putString(PhotoFragment.POSITION, position);
		bundle.putString(PhotoFragment.DEFAULT_IMAGE,
				gallerySpec.getDefaultImage());
		bundle.putString(PhotoFragment.NO_PHOTO, gallerySpec.getNoPhoto());
		fragment.setArguments(bundle);
		return fragment;
	}

	private RecyclingImageView mImageView;
	private ProgressBar mProgressBar;
	private TextView mTextView;

	@Override
	public void onActivityCreated(final Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		final Bundle bundle = getArguments();
		final String imageURL = bundle.getString(PhotoFragment.IMAGE_URL);
		final String noPhoto = bundle.getString(PhotoFragment.NO_PHOTO);
		if (!TextUtils.isEmpty(bundle.getString(PhotoFragment.DEFAULT_IMAGE))) {
			final int resId = ResourcesHelper.getDrawableIdByName(bundle
					.getString(PhotoFragment.DEFAULT_IMAGE));
			ImageFetcher.getInstance().setLoadingImage(resId);
		}
		mTextView.setText(bundle.getString(POSITION));
		if (TextUtils.isEmpty(imageURL)) {
			this.mProgressBar.setVisibility(View.GONE);
			if (!TextUtils.isEmpty(noPhoto)) {
				final int resId = ResourcesHelper.getDrawableIdByName(noPhoto);
				final Drawable drawable = getActivity().getResources()
						.getDrawable(resId);
				this.mImageView.setImageDrawable(drawable);
			}
		} else {
			ImageFetcher.getInstance().loadImage(imageURL, this.mImageView);
		}
	}

	@Override
	public View onCreateView(final LayoutInflater inflater,
			final ViewGroup container, final Bundle savedInstanceState) {
		FrameLayout frameLayout = (FrameLayout) inflater.inflate(
				R.layout.aplink__generic_photoview, container, false);
		mTextView = (TextView) frameLayout.findViewById(R.id.textview_position);
		mProgressBar = (ProgressBar) frameLayout.findViewById(R.id.progressbar);
		mImageView = (RecyclingImageView) frameLayout
				.findViewById(R.id.imageview);
		return frameLayout;
	}

	@Override
	public void onPause() {
		super.onPause();
	}
}
